package managers.dateManager.beginFrozen.beginChange;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.TaskManagerException;
import org.tbjd.client.managers.TaskManager;

import factories.TaskFactory;

/**
 * <p>
 * Test TaskManager's methods
 * </p>
 * <p>
 * <b>Case</b>
 * </p>
 * <p>
 * Task with BeginDate frozen and no relationships (predecessors/successors)
 * </p>
 * <p>
 * <b> Expected</b>
 * <ul>
 * <li>update EndDate (F4)</li>
 * <li>update Successors</li>
 * </ul>
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 */
public class BeginFrozen_BeginChange_UpdateSingleTask_Basic extends TestCase {

	private Task task = null;
	private Map<Integer, Object> map = null;
	private Logger log = Logger.getLogger("console");
	private DateFormat df = DateFormat.getInstance();
	private TaskManager dateManager = new TaskManager();

	protected void setUp() throws Exception {
		df = new SimpleDateFormat("dd.MM.yyyy");

		task = (Task) TaskFactory.getInstance();

		super.setUp();
	}

	public void testReturnedBeanListNotNull() {
		System.out.println("");
		log.debug(" === testReturnedBeanListNotNull");
		log.debug("original BeginDate = " + df.format(task.getBeginDate()));
		log.debug("original EndDate = " + df.format(task.getEndDate()));

		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}
		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		try {
			assertNotNull("Updated bean list isn't null", dateManager.manageDates(map));

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

	}

	public void testOnlyOneBeanInReturnedList() {
		System.out.println("");
		log.debug(" === testOnlyOneBeanInReturnedList");
		log.debug("original BeginDate = " + df.format(task.getBeginDate()));
		log.debug("original EndDate = " + df.format(task.getEndDate()));

		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}
		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}
		assertEquals("Updated bean list size = 1", 1, updatedBeans.size());

	}

	public void testReturnedBeanIsSameAsOriginal() {
		System.out.println("");
		log.debug(" === testReturnedBeanIsSameAsOriginal");
		log.debug("create new task...");

		Task task = (Task) TaskFactory.getInstance();
		// task.setState(StateBeginFrozenSPEC.getInstance());

		log.debug("original BeginDate = " + task.getBeginDate());
		log.debug("original EndDate = " + task.getEndDate());

		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}
		log.debug("new BeginDate = " + newBeginDate);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		assertEquals("Task given and received are equals", task, updatedBeans.get(0));

	}

	public void testBeginDateHasBeenChanged() {
		System.out.println("");
		log.debug(" === testBeginDateHasBeenChanged");
		log.debug("create new task...");

		Task task = (Task) TaskFactory.getInstance();
		// task.setState(StateBeginFrozenSPEC.getInstance());

		Date originalBeginDate = task.getBeginDate();
		Date originalEndDate = task.getEndDate();
		float originalDuration = task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}

		log.debug("original BeginDate = " + originalBeginDate);
		log.debug("original EndDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		assertEquals("Assert beginDate has been udated with new BeginDate", newBeginDate, updatedBeans.get(0).getBeginDate());

	}

	public void testEndDateHasBeenChanged() {
		System.out.println("");
		log.debug(" === testEndDateHasBeenChanged");
		log.debug("create new task...");

		Task task = (Task) TaskFactory.getInstance();
		// task.setState(StateBeginFrozenSPEC.getInstance());

		Date originalBeginDate = task.getBeginDate();
		Date originalEndDate = task.getEndDate();
		float originalDuration = task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		assertNotSame("Assert endDate has been udated", originalEndDate, updatedBeans.get(0).getEndDate());

	}

	public void testDurationHasNotBeenChanged() {
		System.out.println("");
		log.debug(" === testDurationHasNotBeenChanged");
		log.debug("create new task...");

		Task task = (Task) TaskFactory.getInstance();
		// task.setState(StateBeginFrozenSPEC.getInstance());

		Date originalBeginDate = task.getBeginDate();
		Date originalEndDate = task.getEndDate();
		float originalDuration = task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("compute with new BeginDate = " + newBeginDate + "...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		assertEquals("Assert duration hans't changed", originalDuration, updatedBeans.get(0).getDuration());

	}

	public void testRevertGivesOriginalBeginDate() {
		System.out.println("");
		log.debug(" === testRevertGivesOriginalBeginDate");
		log.debug("create new task...");

		Task task = (Task) TaskFactory.getInstance();
		// task.setState(StateBeginFrozenSPEC.getInstance());

		Date originalBeginDate = task.getBeginDate();
		Date originalEndDate = task.getEndDate();
		float originalDuration = task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		log.debug("call dateManager with new BeginDate...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		try {
			dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("call dateManager with original BeginDate...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, originalBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("retrieved BeginDate = " + updatedBeans.get(0).getBeginDate() + " & original BeginDate = " + originalBeginDate);
		assertEquals("Assert BeginDate reverted to original", originalBeginDate, updatedBeans.get(0).getBeginDate());

	}

	public void testRevertGivesOriginalEndDate() {
		System.out.println("");
		log.debug(" === testRevertGivesOriginalEndDate");
		log.debug("create new task...");

		Task task = (Task) TaskFactory.getInstance();
		// task.setState(StateBeginFrozenSPEC.getInstance());

		Date originalBeginDate = task.getBeginDate();
		Date originalEndDate = task.getEndDate();
		float originalDuration = task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		log.debug("call dateManager with new BeginDate = " + newBeginDate + "...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		try {
			dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("call dateManager with original BeginDate = " + originalBeginDate + "...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, originalBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("retrieved EndDate = " + updatedBeans.get(0).getEndDate() + " & original BeginDate = " + originalEndDate);
		assertEquals("Assert BeginDate reverted to original", originalEndDate, updatedBeans.get(0).getEndDate());

	}

	public void testBeginDatesChangedWithASAPChaining() {
		System.out.println("");
		log.debug(" === testBeginDatesChangedWithASAPChaining");
		log.debug("create new task...");

		Task task = (Task) TaskFactory.getInstance();
		// task.setState(StateBeginFrozenSPEC.getInstance());

		Date originalBeginDate = task.getBeginDate();
		Date originalEndDate = task.getEndDate();
		float originalDuration = task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}

		log.debug("original BeginDate = " + originalBeginDate);
		log.debug("original EndDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		assertEquals("Assert beginDate has been udated with new BeginDate", newBeginDate, updatedBeans.get(0).getBeginDate());

	}

	public void testEndDateChangedWithASAPChaining() {
		System.out.println("");
		log.debug(" === testEndDateChangedWithASAPChaining");
		log.debug("create new task...");

		Task task = (Task) TaskFactory.getInstance();
		// task.setState(StateBeginFrozenSPEC.getInstance());

		Date originalBeginDate = task.getBeginDate();
		Date originalEndDate = task.getEndDate();
		float originalDuration = task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		assertNotSame("Assert endDate has been udated", originalEndDate, updatedBeans.get(0).getEndDate());

	}
}